use std::{collections::HashMap, io::Write};

use crate::{lexer::Lexer, parser::Parser};

pub fn start() {
  let mut variables: HashMap<char, f32> = HashMap::new();
  loop {
    print!(">> ");
    std::io::stdout().flush().unwrap();
    let input = {
      let mut buf = String::new();
      std::io::stdin().read_line(&mut buf).unwrap();
      buf
    };

    if input.trim() == "exit" {
      break;
    }

    let lexer = Lexer::new(&input);
    let mut parser = Parser::new(lexer);
    let expr = parser.parse();
    println!("{:?}", expr.to_string());

    if let Some((var_name, lhs)) = expr.is_asign() {
      let value = lhs.eval(&variables);
      variables.insert(var_name, value);
      continue;
    }
    let value = expr.eval(&variables);
    println!("{}", value);
  }
}
